<script>
	
	import {} from '@/mpf/config.js'
	import TokenManager from '@/mpf/token-manager'
	export default {
		globalData:{
		},
		onLaunch: function() {
			// #ifdef MP-WEIXIN
			var options = wx.getLaunchOptionsSync();
			

			// #endif
			
			const systemInfo = uni.getSystemInfoSync();
			var winW = systemInfo.windowWidth;
			var px = winW / 750;
			let menuButtonBoundingClient = {top:0, height:0};
			// #ifndef H5 || APP
			menuButtonBoundingClient = uni.getMenuButtonBoundingClientRect();
			// #endif
			
			this.globalData.windowWidth = systemInfo.windowWidth;
			this.globalData.windowHeight = systemInfo.windowHeight;
			this.globalData.px = px;
			this.globalData.rpx = 750/winW;
			this.globalData.isIphone = systemInfo.model && systemInfo.model.indexOf('iPhone') !== -1;
			this.globalData.statusBarHeight = systemInfo.statusBarHeight || 0;
			
			this.globalData.headerHeight = this.globalData.statusBarHeight  + menuButtonBoundingClient.height + (menuButtonBoundingClient.top - this.globalData.statusBarHeight) * 2;
			this.globalData.headerTop = menuButtonBoundingClient.top;

		
			
			var tmplIds = [];
			

			var onLoginLockd = false;
			// 登录成功触发
			global.onLogin = () => {
				
				if(onLoginLockd){
					console.log("onLogin 重复触发");
					return ;
				}
				onLoginLockd = true;
				setTimeout(() => {
					onLoginLockd = false;
				}, 5000);
				
				this.loadUserInfo();
				
				// #ifdef MP
				uni.getSetting({
				      withSubscriptions: true,
				      success: (res => {
						
						if(tmplIds.length == 0){
							return ;
						}
						 
						  
						var needChoose = false;  // 需要勾选订阅
						var needOpen = false;  // 需要打开设置
						
				        if (!res.subscriptionsSetting || !res.subscriptionsSetting.itemSettings) {
							needChoose = true;
				        }
						else if(!res.subscriptionsSetting.mainSwitch){
							needOpen = true;
						}
						else {
							for(var i=0; i<tmplIds.length; i++){
								if(res.subscriptionsSetting[tmplIds[i]] != 'accept'){
									needOpen = true;
									break;
								}
							}
						}
						
						if(needChoose || needOpen){
							setTimeout(() => {
								global.showConfirm({
									title:'订阅消息',
									content:'消息通知授权',
									confirm:function(res){
										
										if(res.confirm){
											if(needChoose){
												uni.requestSubscribeMessage({
												  tmplIds: tmplIds,
												  success: (res => {
													  console.log("requestSubscribeMessage",res);  
												  }),
												  fail: (err => {
													  console.log(err);  
														wx.openSetting({
														  subscriptionsSetting: true,
														  success: (res => {
														  }),
														  fail: (err => {
															  console.log(err);
															global.toast('打开权限失败,点击右上角进入设置打开权限');
														  })
														})
												  })
												})
											}
											else{
												
												wx.openSetting({
												  subscriptionsSetting: true,
												  success: (res => {
												  }),
												  fail: (err => {
													  console.log(err);
													global.toast('打开权限失败,点击右上角进入设置打开权限');
												  })
												})
												
											}
											
											
										}
									},
									fail:function(err){
										console.log(err);
									}
								})
							}, 1000)
							
						}
						
				    })
				})
				// #endif
				
				
				var pages = getCurrentPages();
				var len = pages.length;
				
				if (len > 1) {
				  var currentPage = pages[len - 2];
				  
				  
				  
				  (currentPage.onLoad || currentPage.onPageLoad || currentPage.init || function(options){})(currentPage.options);
				  (currentPage.onShow || currentPage.onPageShow || function(){})();
				  uni.navigateBack()
				}
				else if(len == 1){
				    var currentPage = pages[len - 1];
				    (currentPage.onLoad || currentPage.onPageLoad|| function(options){})(currentPage.options);
				    (currentPage.onShow || currentPage.onPageShow || function(){})();
					if(currentPage.route.indexOf("login") > -1){
						/* uni.switchTab({
							url:"/pages/order/order"
						}) */
						uni.reLaunch({
							url:"/pages/index/index"
						})
				    }
				}
			}
			
			let userToken = TokenManager.get();
			if(userToken && userToken.user_id){
				this.$store.commit('setLoginUserId', userToken.user_id);
			}
			
			
			if(global.unreadMessageInterval){
				clearInterval(global.unreadMessageInterval)
			}
			
			let historyUnReadCount = null;
			 
			// 轮询未读数
			var pollingUnread = () => {
				let token = TokenManager.get();
				
				if(token && token.access_token){
					this.loadUnRead();
				}
			}
			
	
			//pollingUnread();
			//global.unreadMessageInterval = setInterval(pollingUnread, 5000)

			
			global.getCurrentUser = function(){
				var data = TokenManager.getToken() || {};
				return data.user || {} ;
			}
			
			// 监听网络变化
			uni.onNetworkStatusChange( res => {
				
				this.$store.commit('setNetworkState', res.isConnected);
			});
			
			setTimeout(() => {
				var pages = getCurrentPages();
				var len = pages.length;
				if (len > 0) {
					var currentPage = pages[len - 1];
					if(currentPage.route.startsWith("pages/common") && currentPage.route != 'pages/common/profile'){
						return ;
					}
					
				}
				this.loadUserInfo();
			}, 1000);
			
			
			
		},
		onShow: function() {
			// TODO
		},
		onHide: function() {
			// TODO
		}
	}
</script>

<style>
	@import url("/static/css/iconfont.css");

	
</style>
<style lang="scss">
		@import "@/static/css/main.scss";
</style>
